_package("new_driver.page");

_import("caf.ui.Page");
_import("caf.mui.ScrollView");

_class("FixWrongPage", Page, function(){

	/**
	配置
	*/
	var config = {
		// 获取源URL前缀
		sourceBaseUrl : "http://wz.jxedt.com/test/201004abcd/",
		// 试题来源页面URL - 获取主要config
		testPageUrl : "201004-sxtest.asp",
		// 总题数
		totalCount : "",
		// 当前题目页 尝试读取本地存储
		currentPage : window.localStorage.getItem("nd_fixwrong_current") ? parseInt(window.localStorage.getItem("nd_fixwrong_current")) : 1,
		// 读取题目内容AJAX URL
		readQuestionUrl : "",
		// 答案文案对应表
		answerTextMap : ['A','B','C','D']
	};
	
	/**
	缓存
	*/
	var cache = {

	};

	this._init = function(){
		_super._init.call(this);
		this._scrollview = null;
	};
	this.create = function(parent){
		var obj = this.createTplElement(parent, "fix_wrong.xml");
		this.init(obj);
		return obj;
	};
	this.init = function(){
		_super.init.apply(this, arguments);
		this.initComponents();
		this.initActionElements();
		
		// 初始化scroll
		this._scrollview = new ScrollView(); //实例化scrollview类
		this._scrollview.bind($E("scroll-wrapper"), { //绑定scrollview对应的节点
			"parent": this,
			"id"    : "scroll1"
		});
		this.addListener(this._scrollview, "scrollEnd", this._scrollview, "onDefaultScrollEnd");//绑定默认的滚动结束事件

	
		
	};
	this.dispose = function(){
		_super.dispose.apply(this);
		
		// 释放scrollview对象
		this._scrollview.dispose();
		this._scrollview = null;
	};
	

	/**
	获取主要配置
	*/
	this.getConfig = function(o){
	
		var _this = this,
		method = 'GET',
		params = {
			// 类型：C照
		    'type' : 'c'
		},
		url = config.sourceBaseUrl + config.testPageUrl;  
		
		
		_this._app.netInvoke(method, url, params, 'text', _this, function(result) {  
			

			// 取得题目总数
			/*
			var reg = /var Allcount=(.*)/i; 
			if(reg.test(result)){
				config.totalCount = RegExp.$1;
			};
			*/
			
			config.totalCount = parseInt(config.wrongQuestions.length);
			
			
			
			// 更新UI
			$("#fix-wrong-page #total-count").html(config.totalCount);
			

			
			// 题目数据URL拼装
			reg = /var url="(.*)"/i; 
			if(reg.test(result)){
				config.readQuestionUrl = config.sourceBaseUrl + RegExp.$1;
			};
			
			// 运行自定义事件
			$("#fix-wrong-page").trigger('afterGetConfig');

		});
	
	};

	/**
	获取题目内容
	*/
	this.getQuestion = function(){

		var _this = this,
		method = 'GET',
		params = {
			// 总数
			'myTotal' : config.totalCount,
			// 类型：C照
		    'ltype' : 'c',
			// myId
			'myID' : config.wrongQuestions[config.currentPage-1],
			// myOrder
			'myOrder' : config.wrongQuestions[config.currentPage-1]
		},
		url = config.readQuestionUrl;
		
		// 开始获取
		_this._app.netInvoke(method, url, params, 'text', _this, function(qResult) { 

			// 获取题目内容
			reg = /<div class="question">\n*([\s\n\S.]*)<input type="hidden" name="TrueRe".*/ig; 
			if(reg.test(qResult)){
				var question = RegExp.$1;
				$("#fix-wrong-page #question").html(question);
				$("#fix-wrong-page #question span").html(config.currentPage + ".");
			};

			// 获取正确答案
			reg = /<input type="hidden" name="TrueRe" value="(.*)">/ig; 
			if(reg.test(qResult)){
				config.currentTrueAns = RegExp.$1;
			};
			
			// 获取解释
			reg = /style="display:none"><div class="showmsg">(.*?)<a href="http/ig; 
			if(reg.test(qResult)){
				config.why = RegExp.$1;
			};
			
			
			// 获取选项内容
			reg = /<ul class="Item">\n*([\s\n\S.]*)<\/ul>.*/ig; 
			if(reg.test(qResult)){
				// 优化内容
				var listStr = RegExp.$1.replace(/onclick=".*?"/ig,"");
				$("#fix-wrong-page #question-selector").html('<ul class="Item">' + listStr + '</ul>');
				
				// 绑定事件
				$("#fix-wrong-page #question-selector li").click(function(){
					
					var answerRadio = $(this).find("input")[0];
					
					answerRadio.checked = true;
					
					if(answerRadio.value == config.currentTrueAns){
						_this.showAnswerRight();
					}else{
						_this.showAnswerWrong();
					}
					
					// 更新答题状态
					//_this.updateStatus();
					
				});
				
			};
			
			// 重置UI
			this.resetUI();
			
			// 更新页码
			$("#fix-wrong-page #current-page").html(config.currentPage);
			
			// 存入localstorage
			window.localStorage.setItem("nd_fixwrong_current", config.currentPage );
			
			
		});
	
	};
	
	
	/**
	重置UI
	*/
	this.resetUI = function(){
		$("#fix-wrong-page #why").hide();
		$("#fix-wrong-page #result div").hide();
	};
	
	/**
	显示错误答案标识
	*/
	this.showAnswerRight = function(){
		this.resetUI();
		$("#fix-wrong-page #result .answer-right").fadeIn(300);
	};
	
	/**
	显示正确答案标识
	*/
	this.showAnswerWrong = function(){
		this.resetUI();
		$("#fix-wrong-page #result .answer-wrong").fadeIn(300);
		$("#fix-wrong-page #true-answer").html(config.answerTextMap[config.currentTrueAns-1]);
		$("#fix-wrong-page #why").fadeIn(300).html(config.why);
		
		// 存入错题集本地存储
		//config.wrongQuestions.push(parseInt(config.currentPage));
		//config.wrongQuestions = $.unique(config.wrongQuestions);
		//window.localStorage.setItem("nd_wrong_questions", config.wrongQuestions.join(",") );
		
	};
	
	/**
	更新答题状态
	*/
	this.updateStatus = function(){
		//$("#fix-wrong-page #test-status .wrong-count").html(config.wrongQuestions.length);
		//$("#fix-wrong-page #test-status .rate").html( (( 1 - (parseInt(config.wrongQuestions.length)/parseInt(config.totalCount)) )*100).toFixed(2) );
	};
	
	/**
	重置时候触发
	*/
	this.reset = function(o){
		var _this = this;
		
		// 错题集编号数组
		config.wrongQuestions = window.localStorage.getItem("nd_wrong_questions") ? window.localStorage.getItem("nd_wrong_questions").split(",") : [];
		
		
		// 无错题
		if(config.wrongQuestions.length <= 0){
			$("#fix-wrong-page #test-status").hide();
			$("#fix-wrong-page #scroll-wrapper").hide();
			$("#fix-wrong-page #page-nav").hide();
			$("#fix-wrong-page #no-result").show();
			return;
		}

		// 获取主要配置
		_this.getConfig();
		
		// 取得config之后运行
		$("#fix-wrong-page").bind("afterGetConfig", function(){
			// 获取题目
			_this.getQuestion();
			// 更新答题状态
			//_this.updateStatus();
		});
		

		
	};
	

	
	/**
	面包屑相关
	*/
	this.do_backToWelcome = function(){
		this._app.navPage("welcome");
	};
	
	
	/**
	向后翻页
	*/
	this.do_goNextQuestion = function(){
		if(config.currentPage >= config.totalCount)return;
		// 当前页增加1
		config.currentPage++;
		// 获取题目
		this.getQuestion();
	};
	
	/**
	向前翻页
	*/
	this.do_goPreviousQuestion = function(){
		if(config.currentPage <= 1)return;
		// 当前页增加1
		config.currentPage--;
		// 获取题目
		this.getQuestion();
	};
	
	/**
	跳转到错题集
	*/
	this.do_toFixWrongPage = function(){
		this._app.navPage("fix_wrong");
	};
	
	
	/**
	回到第一题
	*/
	this.do_firstQuestion = function(){

		config.currentPage = 1;
		
		config.wrongQuestions = [];
		
		window.localStorage.setItem("nd_wrong_questions", "" );
		
		//this.updateStatus();
		
		this.getQuestion();
	};
	
	
	
});